<template>
  <div>
    <el-dialog :title="meta" :visible="showDialog" @close="closeForm">
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model.trim="form.username" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model.trim="form.email" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model.trim="form.phone" />
        </el-form-item>
        <el-form-item label="角色" prop="role_id">
          <el-select v-model="form.role_id" placeholder="请选择">
            <el-option label="超级管理员" value="1" />
            <el-option label="管理员" value="2" />
            <el-option label="老师" value="3" />
            <el-option label="学生" value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择">
            <el-option label="启用" value="1" />
            <el-option label="禁用" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="用户备注">
          <el-input v-model="form.remark" />
        </el-form-item>
        <el-form-item style="display: flex; justify-content: center">
          <el-button @click="closeForm">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { addUser, editUser, getUserList } from '../../../api/user'
export default {
  data() {
    var testUsername = async(rule, value, callback) => {
      const res = await getUserList()
      if (res.data.items.find(v => v.username === value)) {
        callback(new Error('用户名重复'))
      } else {
        callback()
      }
    }
    var testPhone = async(rule, value, callback) => {
      const res = await getUserList()
      if (res.data.items.find(v => v.phone === value)) {
        callback(new Error('手机号重复'))
      } else {
        callback()
      }
    }
    var testEmail = async(rule, value, callback) => {
      const res = await getUserList()
      if (res.data.items.find(v => v.email === value)) {
        callback(new Error('邮箱重复'))
      } else {
        callback()
      }
    }
    return {
      meta: '新增用户',
      showDialog: false,
      form: {
        username: '',
        email: '',
        phone: '',
        role_id: '',
        status: '',
        remarks: ''
      },
      formLabelWidth: '120px',
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { validator: testUsername, message: '用户名重复', trigger: 'blur' }
        ],
        email: [{ required: true, message: '请输入邮箱', trigger: 'blur' },
          { validator: testEmail, message: '邮箱重复', trigger: 'blur' },
          { pattern: /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, message: '邮箱格式不对', trigger: 'blur' }],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validator: testPhone, message: '手机号重复', trigger: 'blur' },
          { pattern: /^(?:(?:\+|00)86)?1\d{10}$/, message: '手机号格式不对', trigger: 'blur' }
        ],
        role_id: [{ required: true, message: '请选择角色', trigger: 'change' }],
        status: [{ required: true, message: '请选择状态', trigger: 'change' }]
      }
    }
  },
  methods: {
    submitForm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.meta === '新增用户') {
            const res = await addUser(this.form)
            console.log(res)
            this.$message.success('新增用户成功')
          } else {
            const res = await editUser(this.form)
            console.log(res)
            this.$message.success('编辑用户成功')
          }
          this.closeForm()
          this.$emit('getUserList')
        } else {
          this.message.success('校验不通过')
          return false
        }
      })
    },
    closeForm() {
      this.form = {}
      this.showDialog = false
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep {
  .el-dialog__header {
    background-color: #07b3fa;
    text-align: center;
  }
  .el-dialog__title,
  .el-dialog__close {
    color: #fff;
  }
  .el-dialog__close:hover {
    color: #fff;
  }
}
</style>
